package edu.jptest.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.jptest.bean.TestPoint;
import edu.jptest.util.GetDataBaseConnection;

public class TestPointDAO {
	// 插入考点
	public boolean insertTestPoint(TestPoint testPoint) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "insert into test_point(type,content) values(?,?)";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, testPoint.getType());
			pstm.setString(2, testPoint.getContent());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 删除考点
	public boolean deleteTestPoint(int testPointId) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "delete from test_point where test_point_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, testPointId);
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 更该考点
	public boolean updateTestPoint(TestPoint testPoint) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "update test_point set type=?,content=?where test_point_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, testPoint.getType());
			pstm.setString(2, testPoint.getContent());
			pstm.setInt(3, testPoint.getTestPointId());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 通过考点id，查询考点对象
	public TestPoint getTestPointByTestPointId(int testPoint_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		TestPoint testPoint = new TestPoint();
		PreparedStatement pstm = null;
		try {
			String sql = "select * from test_point where test_point_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, testPoint_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				testPoint.setTestPointId(resultSet.getInt(1));
				testPoint.setType(resultSet.getString(2));
				testPoint.setContent(resultSet.getString(3));
				return testPoint;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return new TestPoint();
	}

	// 通过大考点、小考点，查询考点id
	public int getTestPointIdByContent(String type, String content) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select test_point_id from test_point where type=? and content=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, type);
			pstm.setString(2, content);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				return resultSet.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return 0;
	}

	// 获得所有考点
	public List<TestPoint> getAllTestPoints() {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<TestPoint> testPointList = new ArrayList<TestPoint>();
		try {
			String sql = "select * from test_point";
			pstm = connection.prepareStatement(sql);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				TestPoint testPoint = new TestPoint();
				testPoint.setTestPointId(resultSet.getInt(1));
				testPoint.setType(resultSet.getString(2));
				testPoint.setContent(resultSet.getString(3));
				testPointList.add(testPoint);
			}
			return testPointList;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 获得所有大考点
	public List<String> getTestPointsType() {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<String> types = new ArrayList<String>();
		try {
			String sql = "select distinct type from test_point";
			pstm = connection.prepareStatement(sql);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				types.add(resultSet.getString(1));
			}
			return types;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过大考点，查询所具有的小考点
	public List<String> getTestPointsContentByType(String testpoint_type) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<String> contentsId = new ArrayList<String>();
		try {
			String sql = "select distinct content from test_point where type=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, testpoint_type);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				contentsId.add(resultSet.getString(1));
			}
			return contentsId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

}
